package com.example.mediarecorder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;


import com.example.mediarecorder.MovieRecorderView.OnRecordFinishListener;

/**
 * 视频录制
 * @author 胡汉三
 *
 */
public class MainActivity extends Activity {

	private MovieRecorderView mRecorderView;
	private Button mShootBtn;
	private boolean isFinish = true;
	private String userId = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Intent i_getvalue = getIntent();  
		String action = i_getvalue.getAction();  
		  
		if(Intent.ACTION_VIEW.equals(action)){  
		    Uri uri = i_getvalue.getData();  
		    if(uri != null){  
		        userId = uri.getQueryParameter("userId");  
		    }  
		}
		
		mRecorderView = (MovieRecorderView) findViewById(R.id.movieRecorderView);
		mShootBtn = (Button) findViewById(R.id.shoot_button);

		mShootBtn.setOnTouchListener(new OnTouchListener() {

			@Override 
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					mRecorderView.record(new OnRecordFinishListener() {

						@Override
						public void onRecordFinish() {
							handler.sendEmptyMessage(1);
						}
					});
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					if (mRecorderView.getTimeCount() > 1)
						handler.sendEmptyMessage(1);
					else {
						if (mRecorderView.getmVecordFile() != null)
							mRecorderView.getmVecordFile().delete();
						mRecorderView.stop();
						Toast.makeText(MainActivity.this, "视频录制时间太短", Toast.LENGTH_SHORT).show();
					}
				}
				return true; 
			}
		});
	}


	@Override
	public void onResume() {
		super.onResume();
		isFinish = true;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		isFinish = false;
		mRecorderView.stop();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			finishActivity();
		}
	};

	private void finishActivity() {
		if (isFinish) {
			mRecorderView.stop();
			
			Intent intent = new Intent(MainActivity.this, VideoRead.class);
			// 新建Bundle对象  
			Bundle mBundle = new Bundle();    
			// 放入path参数
			mBundle.putString("path", mRecorderView.getmVecordFile().toString());    
			mBundle.putString("userId", userId);
			intent.putExtras(mBundle);   
			startActivity(intent);  
			
			//VideoPlayerActivity.startActivity(this, mRecorderView.getmVecordFile().toString());
		} 
	} 

	/**
	 * 录制完成回调
	 *
	 * @author 胡汉三
	 * 
	 * @date 2015-2-9 
	 */
	public interface OnShootCompletionListener {
		public void OnShootSuccess(String path, int second);
		public void OnShootFailure();
	}
}